package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.beans.VerCodeInfo;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.UrlAddress;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.views.MyToast;

import java.util.regex.Pattern;

/**
 * Created by william_sun on 2016-07-06.
 * Description:注册填写手机号
 */
public class RegisterPhoneActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private LinearLayout ll_phone;
    private ImageView iv_phone;
    private EditText et_phone;
    private LinearLayout ll_pw;
    private ImageView iv_pw;
    private EditText et_pw;
    private LinearLayout ll_code;
    private EditText et_code;
    private Button bt_getcode;
    private CheckBox cb;
    private TextView tv_agreement;
    private Button bt_register;

    private MyToast myToast;

    private boolean phoneInput = false;
    private boolean pwInput = false;
    private boolean codeInput = false;

    private ObjectResultBean<VerCodeInfo> getCodeResultBean;

    private ObjectResultBean<VerCodeInfo> checkCodeResultBean;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETCODE_SUCCESS:
                timer.start();
                    toastMsg(mContext.getResources().getString(R.string.getverifysuccess));
                break;
            case MessageCode.NETWORKCODE_GETCODE_FAILURE:
                bt_getcode.setEnabled(true);
                if (msg.obj != null && !TextUtils.isEmpty((String) msg.obj)) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(mContext.getResources().getString(R.string.getverifyfail));
                }

                break;
            case MessageCode.NETWORKCODE_CHECKCODE_SUCCESS:
                if (msg.obj != null) {
                    checkCodeResultBean = (ObjectResultBean<VerCodeInfo>) msg.obj;
                /*    Intent iRegister = new Intent(mContext, RoleSelectActivity.class);
                    iRegister.putExtra("tel", et_phone.getText().toString());
                    iRegister.putExtra("password", et_pw.getText().toString());
                    startActivity(iRegister);*/
                    Intent iNurse = new Intent(mContext, PersonalInfoForDNActivity.class);
                    iNurse.putExtra("tel", et_phone.getText().toString());
                    iNurse.putExtra("password", et_pw.getText().toString());
                    startActivity(iNurse);

                    myToast.setLongMsg(checkCodeResultBean.getMessage());
                    return;
                }

                break;
            case MessageCode.NETWORKCODE_CHECKCODE_FAILURE:
                if (msg.obj != null && !TextUtils.isEmpty((String) msg.obj)) {
                    //测试跳转使用
                    myToast.setLongMsg((String) msg.obj);
                } else {
                    myToast.setLongMsg(mContext.getResources().getString(R.string.wrong_code));
                }

                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registerphone);
        initView();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        ll_phone = (LinearLayout) findViewById(R.id.ll_registerphone_phone);
        iv_phone = (ImageView) findViewById(R.id.iv_registerphone_phone);
        et_phone = (EditText) findViewById(R.id.et_registerphone_phone);
        ll_pw = (LinearLayout) findViewById(R.id.ll_registerphone_pw);
        iv_pw = (ImageView) findViewById(R.id.iv_registerphone_pw);
        et_pw = (EditText) findViewById(R.id.et_registerphone_pw);
        ll_code = (LinearLayout) findViewById(R.id.ll_registerphone_code);
        bt_getcode = (Button) findViewById(R.id.bt_registerphone_code);
        et_code = (EditText) findViewById(R.id.et_registerphone_code);
        cb = (CheckBox) findViewById(R.id.cb_registerphone);
        tv_agreement = (TextView) findViewById(R.id.tv_registerphone_agreement);
        bt_register = (Button) findViewById(R.id.bt_registerphone);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(getResources().getText(R.string.register));

        LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, screenWidth / 8);
        ll_phone.setLayoutParams(lp_ll);
        ll_code.setLayoutParams(lp_ll);
        ll_pw.setLayoutParams(lp_ll);
        LinearLayout.LayoutParams lp_phone = new LinearLayout.LayoutParams(screenWidth / 16, screenWidth / 16);
        lp_phone.rightMargin = ImageUtil.dip2px(30, this);
        iv_phone.setLayoutParams(lp_phone);
        iv_pw.setLayoutParams(lp_phone);
        LinearLayout.LayoutParams lp_etcode = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp_etcode.leftMargin = ImageUtil.dip2px(60, this) + screenWidth / 16;
        lp_etcode.weight = 1;
        et_code.setLayoutParams(lp_etcode);
        bt_getcode.getLayoutParams().width = screenWidth / 3;
        ((LinearLayout.LayoutParams) cb.getLayoutParams()).leftMargin = ImageUtil.dip2px(30, this);
        tv_agreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    phoneInput = false;
                    bt_register.setBackgroundResource(R.drawable.bt_grey);
                    bt_register.setEnabled(false);
                } else {
                    phoneInput = true;
                    if (codeInput && pwInput) {
                        bt_register.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_register.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_pw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    pwInput = false;
                    bt_register.setBackgroundResource(R.drawable.bt_grey);
                    bt_register.setEnabled(false);
                } else {
                    pwInput = true;
                    if (codeInput && phoneInput) {
                        bt_register.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_register.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    codeInput = false;
                    bt_register.setBackgroundResource(R.drawable.bt_grey);
                    bt_register.setEnabled(false);
                } else {
                    codeInput = true;
                    if (phoneInput && pwInput) {
                        bt_register.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        bt_register.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        bt_getcode.setEnabled(true);
        bt_getcode.setOnClickListener(this);
        cb.setChecked(true);
        tv_agreement.setOnClickListener(this);
        bt_register.setEnabled(false);
        bt_register.setOnClickListener(this);

        myToast = new MyToast(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            case R.id.bt_registerphone_code:
                String phone = et_phone.getText().toString().trim();
                if (!StringUtil.isTelephonenum(phone)) {
                    myToast.setLongMsg(R.string.inputrightphonenum);
                } else {
                    bt_getcode.setEnabled(false);
                    YiJiaHuNetWorkUtils.getCode(phone, handler);
                }
                break;
            case R.id.tv_registerphone_agreement:
                Intent intent = new Intent(RegisterPhoneActivity.this, WebActivity.class);
                intent.putExtra("url", UrlAddress.H5_TIAOKUAN_URL);
                intent.putExtra("title", mContext.getResources().getString(R.string.agreement));
                startActivity(intent);
                break;
            case R.id.bt_registerphone:
                StringUtil.zhugeTrack(mContext, "注册", "注册按钮点击数");
                String pw = et_pw.getText().toString().trim();
                // (?![0-9]+$)表示以后的字符不是全数字，(?![a-zA-Z]+$)表示后面的字符不是全字母，[0-9A-Za-z]{6,16}表示数字或字母6-16位
//                if (!Pattern.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,11}$", pw)) {
                if (!Pattern.matches("^[a-zA-Z0-9]{6,11}$", pw)) {
                    toastMsg(R.string.input_right_pw);
                    return;
                }
                if (et_code.length() < 6) {
                    myToast.setLongMsg(R.string.input_right_code);
                    return;
                }
                if (!cb.isChecked()) {
                    myToast.setLongMsg(R.string.checkbox_tip);
                    return;
                }
                loading();
                YiJiaHuNetWorkUtils.getCheckCode(et_phone.getText().toString(), et_code.getText().toString(), handler);
                break;
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            bt_getcode.setText((millisUntilFinished / 1000) + "S");
            bt_getcode.setEnabled(false);
            bt_getcode.setBackgroundResource(R.color.grey_button);
        }

        @Override
        public void onFinish() {
            bt_getcode.setEnabled(true);
            bt_getcode.setText(R.string.getcode);
            bt_getcode.setBackgroundResource(R.color.light_green);
        }
    };

}
